namespace Ccr
{
    public class Reply<T> 
    {
        private PortSet<T, Exception> m_port = new PortSet<T, Exception>();
        private ManualResetEventSlim m_mre = new ManualResetEventSlim();

        public T Wait()
        {
            if (!m_mre.Wait(1000))
                throw new Exception("Actor operation timeout");
            
            T result = (T)m_port;
            if (result != null)
                return result;

            Exception ex = (Exception)m_port;
            if (ex != null)
                throw ex;

            throw new InvalidOperationException();
        }
              
        public void Post(T result)
        {
            m_port.Post(result);    
            m_mre.Set();
        }
        public void Post(Exception ex)
        {
            m_port.Post(ex);
            m_mre.Set();
        }
    }
}
